package com.designpattern.singleton.innerclasssingleton;

import java.io.ObjectStreamException;
import java.io.Serializable;

//静态内部类
public class InnerClassSingleton implements Serializable {
//    static final long serialVersionUID = 42L;
    private static class InnerClassSingletonHolder {
        private static InnerClassSingleton instance = new InnerClassSingleton();
    }

    private InnerClassSingleton() {
        if (InnerClassSingletonHolder.instance!=null) {
            throw new RuntimeException("单例不允许创建多个实例");
        }
    }
    public static InnerClassSingleton getInstance(){
        return InnerClassSingletonHolder.instance;
    }
    Object readResolve() throws ObjectStreamException {
        return InnerClassSingletonHolder.instance;
    }
}
